package br.schindler.android.wallpaper.milkyway;

import java.util.Map;

import android.content.SharedPreferences;
import android.util.Log;
import android.view.MotionEvent;
import br.schindler.android.wallpaper.R;
import br.schindler.games.android.gl.base.GLBackground;
import br.schindler.games.android.gl.base.GLWorld;
import br.schindler.games.android.gl.wallpaper.GLWallpaperService;

public class WallpaperMilkyWay extends GLWallpaperService {
	static final String SHARED_PREFS_NAME = "WallpaperMilkyway";
	
	private GLWorld renderer;
	private SolarSystem milkyway;
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * br.schindler.games.android.gl.wallpaper.GLWallpaperService#onCreateEngine
	 * ()
	 */
	@Override
	public Engine onCreateEngine() {
		renderer = new GLWorld();	
		SharedPreferences time = getSharedPreferences("time", MODE_PRIVATE);		
		renderer.addMesh(new GLBackground(this, R.drawable.texture_bg));
		milkyway=new SolarSystem(this);
		renderer.addMesh(milkyway);
		if (time != null)
			milkyway.setTime(time.getLong("start_time", 0));
		else
			onTimePause();
		return new MilkywayEngine();
	}
	
    /**
     * 
     */
    protected void onTimePause(){
       	SharedPreferences time = getSharedPreferences("solar_timer", MODE_PRIVATE);
    	SharedPreferences.Editor edit = time.edit();
    	Long t =  milkyway.getTime();
    	edit.putLong("start_time", t);
    	edit.commit();
    	Log.d("WALLPAPER ENGINE", "save time: " + t);
    }
    
    /**
     * 
     */
    protected void onTimeResume(){
		SharedPreferences time = getSharedPreferences("solar_timer", MODE_PRIVATE);
		
		if(null!=time){
		  milkyway.setTime(time.getLong("start_time", 0));
		  Log.d("WALLPAPER ENGINE", "load time: " + time.getLong("start_time", 0));
		}
		Log.d("WALLPAPER ENGINE", "load out");
    }
	
	/**
	 */
	class MilkywayEngine extends GLEngine implements SharedPreferences.OnSharedPreferenceChangeListener {
		
	    private float mPreviousX;
	    private float mPreviousY;
	    
	    private final float TOUCH_SCALE_FACTOR = 180.0f / 320;	    
		private float mAngleX = 0, mAngleY = -20;
		
		private MilkywayEngine (){
			setRenderer(renderer);
			setRenderMode(RENDERMODE_CONTINUOUSLY);
			SharedPreferences mPrefs = WallpaperMilkyWay.this.getSharedPreferences(SHARED_PREFS_NAME, 0);
	        mPrefs.registerOnSharedPreferenceChangeListener(this);
	        onSharedPreferenceChanged(mPrefs, null);
		}

		/*
		 * (non-Javadoc)
		 * @see android.content.SharedPreferences.OnSharedPreferenceChangeListener#onSharedPreferenceChanged(android.content.SharedPreferences, java.lang.String)
		 */
		@Override
		public void onSharedPreferenceChanged(
				SharedPreferences prefs, String key) {
			if (null != key) {
				Log.d("pref key: ",key);
				
				if (key.equals("orbitsOn"))
					milkyway.showOrbit(prefs.getBoolean(key, true));
				else
				if (key.equals("viewMode"))
					milkyway.setViewMode(Integer.parseInt(prefs.getString(key, "1")));
				else
				if (key.equals("timeDay"))
					milkyway.setTimeMode(Integer.parseInt(prefs.getString(key, "5")));
			
				Log.d("pref key", "pref set");
			}
			else{
				Map <String, ?> all = prefs.getAll();
				
				for(String k : all.keySet()){
					onSharedPreferenceChanged(prefs, k);
				}
			}
		}
		
	    /*
         * Store the position of the touch event so we can use it for drawing later
         */
        @Override
        public void onTouchEvent(MotionEvent e) {
        	
            float x = e.getX();
            float y = e.getY();
            
            switch (e.getAction()) {
            case MotionEvent.ACTION_MOVE:
                float dx = x - mPreviousX;
                float dy = y - mPreviousY;
     
                mAngleX += dx * TOUCH_SCALE_FACTOR;
                mAngleY += dy * TOUCH_SCALE_FACTOR; 
                
                renderer.setPortViewAngle(mAngleX, mAngleY);
            }
            
            mPreviousX = x;
            mPreviousY = y;
            
            super.onTouchEvent(e);
        }		
        
		/*
		 * (non-Javadoc)
		 * 
		 * @see
		 * br.schindler.games.android.gl.wallpaper.GLWallpaperService.GLEngine
		 * #onPause()
		 */
        @Override
        public void onPause() {
        	super.onPause();
        	onTimePause();
     
        }
        
		/*
		 * (non-Javadoc)
		 * 
		 * @see
		 * br.schindler.games.android.gl.wallpaper.GLWallpaperService.GLEngine
		 * #onResume()
		 */
		@Override
		public void onResume() {
			super.onResume();
			onTimeResume();	
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.service.wallpaper.WallpaperService#onDestroy()
	 */
	@Override
	public void onDestroy() {
		super.onDestroy();
	}
}
